package com.helloniu.controller {

import com.helloniu.fl.NCHandler;
import com.helloniu.fl.SOHandler;
import com.helloniu.fl.util.Tool;
import com.helloniu.model.ChatModel;

public class ChatController extends AbsController {
	public function ChatController() {
		super();
		
		model = new ChatModel();
	}
	
	public static function getIns():ChatController {
		if(controller == null) {
			controller = new ChatController();
		}

		return controller;
	}
		
	override public function initConnection(ancHandler:NCHandler, asoHandler:SOHandler, auserID:String):void {
		if(inited) {
			return;
		}

		inited = true;
		super.initConnection(ancHandler, asoHandler, auserID);
		this.soHandler.addChangeListener(statusChange);   
	}
	
	private function statusChange(asoHandler:SOHandler, pname:String = null, 
		oldValue:* = null, newValue:* = null):void 
	{
	    if(pname == null) {
	        return;
	    }
	
	    newValue = asoHandler.getProperty(pname);
	
		if(pname == "ChatText") {
	        processChatChange(newValue);
	    }
	}

	private function processChatChange(val:*):void {
		model.addChatString(val + "");
		
		// update view
		updateViews();
	}

	public function sendChatToAll(str:String):void {
		soHandler.setProperty("ChatText", str);
	}
	
	public function addChatString(anewStr:String):void {
		anewStr = _userID + ":" + anewStr;
		
		sendChatToAll(anewStr);		
	}
	
	private static var controller:ChatController;
}
}